package com.xzx.study.drawable;

import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.xzx.study.R;

public class MetrixActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText matrix_scale_et;
    private EditText matrix_rotate_et;
    private EditText matrix_translatex_et;
    private EditText matrix_translatey_et;

    private Button matrix_scale_btn;
    private Button matrix_rotate_btn;
    private Button matrix_translate_btn;
    private Button matrix_reset_btn;

    private ImageView matrix_img;

    private Matrix matrix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_metrix);

        matrix_scale_et = findViewById(R.id.matrix_scale_et);
        matrix_rotate_et = findViewById(R.id.matrix_rotate_et);
        matrix_translatex_et = findViewById(R.id.matrix_translatex_et);
        matrix_translatey_et = findViewById(R.id.matrix_translatey_et);

        matrix_scale_btn = findViewById(R.id.matrix_scale_btn);
        matrix_rotate_btn = findViewById(R.id.matrix_rotate_btn);
        matrix_translate_btn = findViewById(R.id.matrix_translate_btn);
        matrix_reset_btn = findViewById(R.id.matrix_reset_btn);

        matrix_img = findViewById(R.id.matrix_img);

        matrix_scale_btn.setOnClickListener(this);
        matrix_rotate_btn.setOnClickListener(this);
        matrix_translate_btn.setOnClickListener(this);
        matrix_reset_btn.setOnClickListener(this);

        matrix = new Matrix();
    }

    @Override
    public void onClick(View v) {
        if (v == matrix_scale_btn) {
            float dx = Float.parseFloat(matrix_scale_et.getText().toString());
            matrix.postScale(dx, dx);
            matrix_img.setImageMatrix(matrix);
        } else if (v == matrix_rotate_btn) {
            float degress = Float.parseFloat(matrix_rotate_et.getText().toString());
            matrix.postRotate(degress);
            matrix_img.setImageMatrix(matrix);
        } else if (v == matrix_translate_btn) {
            float dx = Float.parseFloat(matrix_translatex_et.getText().toString());
            float dy = Float.parseFloat(matrix_translatey_et.getText().toString());
            matrix.postTranslate(dx, dy);
            matrix_img.setImageMatrix(matrix);
        } else if (v == matrix_reset_btn) {
            matrix.reset();
            matrix_img.setImageMatrix(matrix);
        }
    }
}
